#!/usr/bin/env python
# coding=utf-8
# Created on 2014-1-17
#
from model.model import *
from model.mail import send_mail
#from tornado import ioloop, web, httpserver
#from apscheduler.scheduler import Scheduler
from config.config import __VERSION__, PORT, initLog, CBGW_ADDR, BRAND, SMSNAME, SMSPWD, CALL_RATE, SMS_RATE, UNIT, CRENT,config_data
import urllib2, urllib, math, httplib, json
#from model import redis_model
import threading
import thread

# 当前日期
def get_dt():
    return time.strftime('%Y-%m-%d', time.localtime(time.time()))

# 当前时间段
def get_tt():
    return time.strftime('%H', time.localtime(time.time()))

#如果不知道dict中是否有key的值  
  
# dict.get(key)  
  
# 如果用 dict[key] 读取会报KeyError异常  
# # 版本信息
# class versionHandler(web.RequestHandler):
#     def get(self):
#         self.finish(__VERSION__)

#===============================SMS TASK=======================================

def tongjibinfa():
    pass

#抓取钻石供应连的数据
def get_cont(i):
    httpClient = None
    try:
        params = urllib.urlencode({'secret_key': 'd7772b0674a318d5', 'page': i})
        headers = {"Content-type": "application/x-www-form-urlencoded"
                        , "Accept": "text/plain"}

        httpClient = httplib.HTTPConnection("diacloud.test", 80, timeout=30)
        httpClient.request("POST", "/api/diamonds", params, headers)

        response = httpClient.getresponse()
        #print response.status    请求返回状态
        #print response.reason    请求结果 OK
        #print response.read()    请求的内容
        #print response.getheaders() #获取头信息

        if(response.status == 200):
            return response.read()
        else:
            return '';
    except Exception, e:
        print e
    finally:
        if httpClient:
            httpClient.close()

def parase_data(post):
    if not post: return False
    data = {};
    # data['dia_id'] =  '' 
    data['dia_source'] =  'india' 
    data['dia_sup_id'] =  u'10001' if not post.get('dia_sup_id') else post.get('dia_sup_id')  ;
    data['dia_item_no'] = u'' if not post.get('stock_num') else post.get('stock_num'); #供应商货号
    data['dia_place'] = u'' if not post.get('place') else  post.get('place'); #地点
    data['dia_is_using'] = u'' if not post.get('dia_is_using') else post.get('dia_is_using'); #地点
    data['dia_shape'] = u'0' if not (post.get('shape') and config_data.get('shape').get(post.get('shape'))) else config_data.get('shape').get(post.get('shape')) ; #形状
    data['dia_carat'] = u'200' # if not (post['size']) else post['size'] * 100 ; #重量
    data['dia_color'] = u'0' if not ((post.get('color')) and (config_data['color'][post.get('color')])) else config_data['color'][post.get('color')] ; #颜色
    data['dia_clarity'] = u'0' if not ((post.get('clarity')) and (config_data['clarity'][post.get('clarity')])) else config_data['clarity'][post.get('clarity')] ; #净度
    data['dia_cut'] = u'0' if not ((post.get('cut')) and (config_data['cut'][post.get('cut')])) else config_data['cut'][post.get('cut')]; #切工
    data['dia_polish'] = u'0' if not ((post.get('polish')) and (config_data['polish'][post.get('polish')])) else config_data['polish'][post.get('polish')]; #抛光
    data['dia_symmetry'] = u'0' if not (post.get('symmetry') and (config_data['symmetry'][post.get('symmetry')])) else config_data['symmetry'][post.get('symmetry')];     #对称
    data['dia_color_intensity'] = u'0' if not (post.get('fancy_color_intensity') and config_data.get('color_intensity').get(post.get('fancy_color_intensity'))) else config_data.get('color_intensity').get(post.get('fancy_color_intensity'));     #彩钻-强度
    data['dia_color_overtone'] = u'0'; #彩钻-光彩
    data['dia_color_color'] = u'0' if not (post.get('fancy_color') and config_data.get('color_color').get(post.get('fancy_color'))) else config_data.get('color_color').get(post.get('fancy_color'));   #彩钻-光彩
    data['dia_fluorescence'] = u'0' if not (post.get('flr_intensity') and config_data.get('fluorescence').get(post.get('flr_intensity'))) else config_data.get('fluorescence').get(post.get('flr_intensity'))    #荧光颜色

    data['dia_diameter'] = u'' if not post.get('measurement') else  post.get('measurement').replace('x','-'); #尺寸
    data['dia_colsh'] =  u'0'if not (post.get('shade') and config_data.get('colsh').get(post.get('shade'))) else config_data.get('colsh').get(post.get('shade')); #咖色
    data['dia_milky'] =  u'0' if not (post.get('milky') and config_data.get('milky').get(post.get('milky'))) else config_data.get('milky').get(post.get('milky')); #奶色
    data['dia_table_inc'] =  u'0' if not (post.get('table_inc') and config_data.get('table_inc').get(post.get('table_inc'))) else config_data.get('table_inc').get(post.get('table_inc')); #台面瑕疵
    data['dia_black_inc'] =  u'0' if not (post.get('black_inc') and config_data.get('black_inc').get(post.get('black_inc'))) else config_data.get('black_inc').get(post.get('black_inc'))   #黑点
    data['dia_eyeclean'] = '' if not (post.get('eyeclean')) else post.get('eyeclean') ; #裸眼净度
    data['dia_ha'] =  u'0' if (post.get('H&A') and config_data.get('ha').get(post.get('H&A'))) else config_data.get('ha').get(post.get('H&A'))  #心箭
    data['dia_report'] =  u'0' if not (post.get('lab') and config_data.get('report').get(post.get('lab'))) else config_data.get('report').get(post.get('lab'))   #证书
    data['dia_report_no'] = '' if not (post.get('cert_num')) else post.get('cert_num') #证书号
    data['dia_discount'] = u'0' #0 if post.get('sellerid') else (post.get('discount') + 100)*100+sup_data[post.get('sellerid')] ; #进货折扣
    if(post.get('color') != 'C'):
        data['dia_base_discount'] = u'300'; #基础折扣
    else:
        data['dia_base_discount'] = u'200'; #基础折扣
    
    data['dia_image'] = u'' if not (post.get('image')) else post.get('image') ;   #图片
    data['dia_img_flag'] = u'0' if not (post.get('image')) else 1 ;   #图片
    data['dia_video'] = u'0' if not (post.get('video')) else 1 ;   #视频
    data['dia_note'] = u'0' if not (post.get('note')) else 1 ;   #备注
    data['dia_user_id'] = u'0' if not (post.get('sellerid')) else 1 ;   #备注
    data['dia_is_using'] = 1 if not post.get('state') else (1 if not (post.get('state')=='G'or post.get('state')=='M') else u'0') ; #N :不可用  G:可用  M:外借
    data['dia_dollar'] = u'0' if not (post.get('rap')) else post.get('rap'); 
    data['dia_price'] = u'0' if not (post.get('total_price')) else post.get('total_price'); #国际报价
    data['dia_time'] = int(time.time()); #上传时间
    data['dia_is_stock'] = u'0'; #是否现货
    return data

def creat_sql():
    columns = ( 'dia_sup_id', 'dia_source', 'dia_item_no', 'dia_place','dia_is_using', 'dia_is_stock', 'dia_shape', 'dia_carat', 'dia_color', 'dia_clarity', 'dia_cut', 'dia_polish', 'dia_symmetry', 'dia_fluorescence', 'dia_diameter',
            'dia_colsh', 'dia_milky', 'dia_table_inc', 'dia_black_inc', 'dia_report', 'dia_report_no', 'dia_img_flag','dia_base_discount', 'dia_discount', 'dia_dollar', 'dia_image', 'dia_movie', 'dia_note')

    up_cs = []          #组合更新操作，如果数据重复
    for n in columns:
        up_cs.append("%s=VALUES(%s)"%(n, n));

    up_sql= " ON DUPLICATE KEY UPDATE %s" %(','.join(up_cs));

    sql = "INSERT INTO kcd_diamond_cloud (`dia_sup_id`, `dia_source`, `dia_item_no`, `dia_place`,`dia_is_using`, `dia_is_stock`, `dia_shape`, `dia_carat`, `dia_color`, `dia_clarity`, `dia_cut`, `dia_polish`, `dia_symmetry`, `dia_fluorescence`, `dia_diameter`,`dia_colsh`, `dia_milky`, `dia_table_inc`, `dia_black_inc`, `dia_report`, `dia_report_no`, `dia_img_flag`,`dia_base_discount`, `dia_discount`, `dia_dollar`, `dia_image`, `dia_movie`, `dia_note`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) "+up_sql

    i=1
    while True:
        param=[]
        data = get_cont(i)
        data = json.loads(data)
        if not data['data']:   # 没有数据直接跳出
            break
        for h in data['data']:
            tmp = []
            tmp_data = parase_data(h)
            for n in columns:
                # 第一列username，第二列salt，第三列pwd  
                d = tmp_data.get(n) if tmp_data.get(n) else u''
                tmp.append(d) 
            # logger.info(u"获取单条数据为：%s" %tmp)
            param.append(tmp)
        logger.info(u"获取第%s页数据" % i)
        # logger.info(u"获取数据为：%s" % json.dumps(data))
        i+=1
        up_diamond_data(sql, param)
        # add_diamond_data(sql);
    logger.info(u"数据抓取结束")



#获取供应商的数据
def insert_data(data):
    sup_data = query_get_data("SELECT DISTINCT(sup_cloud_id),sup_discount,sup_id,sup_buyer_id FROM kcd_supplier where sup_cloud_id!=''");
    sup_res = {};
    for n in sup_data:
        sup_res[n['sup_cloud_id']]=n['sup_id']


    
def main():
    initLog()
    # data = get_cont()
    # data = json.loads(data)
    # for n in data['data']:
    #     print parase_data(n)
    creat_sql()
    # send_mail(to_email)
        
if __name__ == "__main__":
    main()

